import 'dart:convert';

class QxEntity {
  dynamic bcIds;
  List<QxEntity> children;
  bool checked;
  dynamic attributes;
  int id;
  String text;
  String iconURL;
  dynamic state;
  dynamic iconCls;
  dynamic url;

  QxEntity(
      {this.bcIds,
      this.children,
      this.checked,
      this.attributes,
      this.id,
      this.text,
      this.iconURL,
      this.state,
      this.iconCls,
      this.url});

  QxEntity.fromJson(Map<String, dynamic> json) {
    bcIds = json['bcIds'];


    try {
      List<dynamic> childrens = json['children'];

      if (childrens == null || childrens.length == 0) {
        children = null;
      }else {
        children = childrens.map((item)=> QxEntity.fromJson(item)).toList();
      }
    }catch(e) {
      children = null;
    }



//    try {
//      print('QxEntity.fromJson :'+json['children']);
//      children = jsonDecode(json['children'])
//          .map((item) => QxEntity.fromJson(item))
//          .toList;
//    } catch (e) {
//      print(e.toString());
//      children = null;
//    }
    checked = json['checked'];
    attributes = json['attributes'];
    id = json['id'];
    text = json['text'];
    iconURL = json['iconURL'];
    state = json['state'];
    iconCls = json['iconCls'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['bcIds'] = this.bcIds;
    var childrens = <Map<String,dynamic>>[] ;
    if (children != null) {
      for (int i = 0; i < children.length; i++) {
        Map<String,dynamic> item = children[i].toJson();
        childrens.add(item);
      }
    }
    data['children'] = jsonEncode(childrens);
    data['checked'] = this.checked;
    data['attributes'] = this.attributes;
    data['id'] = this.id;
    data['text'] = this.text;
    data['iconURL'] = this.iconURL;
    data['state'] = this.state;
    data['iconCls'] = this.iconCls;
    data['url'] = this.url;
    return data;
  }

  @override
  String toString() {
    // TODO: implement toString
    return "{id:$id , text: $text}";
  }
}
